package cn.echo.modules.sys.controller.feedbackcontroller;

import cn.echo.modules.sys.entity.Article;
import cn.echo.modules.sys.entity.FeedBack;
import cn.echo.modules.sys.service.ArticleService;
import cn.echo.modules.sys.service.FeedBackService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * Created by Administrator on 2017/4/21.
 */
@Controller
@RequestMapping("/feedback")
public class FeedBackController {

    @Autowired
    FeedBackService feedBackService;
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public String add(FeedBack feedBack){
        feedBack.setId(UUID.randomUUID().toString().replace("-",""));
        feedBack.setCreateDate(new Date());
        feedBackService.add(feedBack);
        return "redirect:findAll";
    }

    @RequestMapping(value = "remove",method = RequestMethod.GET)
    public String remove(FeedBack feedBack){
        feedBackService.remove(feedBack);
        return "redirect:findAll";
    }

    @RequestMapping(value = "toUpdate",method = RequestMethod.GET)
    public String toUpdate(@RequestParam("id") String id, Model model){
        FeedBack feedBack = new FeedBack();
        feedBack.setId(id);
        FeedBack feedBack1 = feedBackService.findById(feedBack);
        model.addAttribute("feedBack",feedBack1);
        return "sys/feedback/updatefeedback";
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(FeedBack feedBack){


        feedBackService.update(feedBack);
        return "redirect:findAll";
    }

    @RequestMapping(value ="findAll",method = RequestMethod.GET)
    public String findAll(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                          Model model){
        PageHelper.startPage(pageNum, pageSize);
        Page<FeedBack> feedBackList = (Page) feedBackService.findAll(new FeedBack());
        PageInfo<FeedBack> pageInfo = new PageInfo(feedBackList);
        model.addAttribute("feedBackList",feedBackList);
        model.addAttribute("page",pageInfo);
        return "sys/feedback/showfeedback";
    }

//    @RequestMapping(value ="findAll0",method = RequestMethod.GET)
//    public String findAll0( @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize")Integer pageSize,Model model){
//        FeedBackService feedBackService = new FeedBackService();
//        PageHelper.startPage(pageNum, pageSize);
//        Page<FeedBack> feedBackList = (Page) feedBackService.findAll(new FeedBack());
//        PageInfo<FeedBack> pageInfo = new PageInfo(feedBackList);
//        model.addAttribute("page",pageInfo);
//        return "sys/feedback/showfeedback";
//    }

    @RequestMapping(value = "toAdd",method = RequestMethod.GET)
    public String toAdd(){
        return  "sys/feedback/addfeedback";
    }

}
